package com.example.yiba8.myapplication;
import android.os.Bundle;
import android.support.v13.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.app.Fragment ;
import android.app.FragmentManager ;

import com.example.yiba8.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {

    private ViewPager viewPager ;
    private List<Fragment> fragments = new ArrayList<Fragment>();
    private Fragment1 fragment1 ;
    private Fragment2 fragment2 ;
    private Fragment3 fragment3 ;

    private ListFragmentPagerAdapter adapter ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main) ;

        viewPager = (ViewPager) findViewById( R.id.viewPager );

        fragment1 = new Fragment1();
        fragment2 = new Fragment2();
        fragment3 = new Fragment3();

        fragments.add( fragment1 ) ;
        fragments.add( fragment2 ) ;
        fragments.add( fragment3 ) ;

        adapter = new ListFragmentPagerAdapter( getFragmentManager() , fragments ) ;
        viewPager.setAdapter( adapter );

        //设置缓存个数
        viewPager.setOffscreenPageLimit( 2 );
    }

    class ListFragmentPagerAdapter extends FragmentPagerAdapter {
        private String[] strtitle = new String[]{"booster", "wifi", "tool"};
        private List<Fragment> mFragmentList;

        public ListFragmentPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
            super(fm);
            this.mFragmentList = fragments;
        }

        @Override
        public Fragment getItem(int position) {
            return  mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList == null ? 0 : mFragmentList.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return strtitle[position];
        }

    }



}
